﻿using System;
using System.Collections.Generic;
using System.Text;
using Catalina.Common;
using System.Collections.ObjectModel;
using Catalina.Dal;
using System.Data;
using POSPlus.EZBarTender;
using Seagull.BarTender.Print;
using System.Windows.Forms;

namespace Catalina.MVP
{
	public static class CatalinaService
	{
		public static Collection<CatalinaHeader> GetHeaders(String connectionString)
		{
			CatalinaHeaderReader reader = new CatalinaHeaderReader(connectionString);
			return reader.Execute();			
		}

		internal static Collection<CatalinaLabel> GetLabels(string connectionString, string startDate)
		{
			CatalinaLabelReader reader = new CatalinaLabelReader(connectionString, startDate);
			return reader.Execute();
		}

		public static void BulkInsert(String corpConnectionString, String localConnectionString)
		{
			CatalinaLabelPersister persister = new CatalinaLabelPersister(corpConnectionString, localConnectionString);
			persister.BulkInsert();
		}

		public static void WriteXml(String path, String labelPath, List<CatalinaLabel> labels)
		{
			BarTenderTreeBuilder builder = new BarTenderTreeBuilder(path, "");
			builder.LabelLocation = labelPath;
			builder.StartTextData();
			String fields = "\"Upc\",\"ProductDescription\",\"Requirements\",\"Incentive\",\"EndDate\",\"TagSubscription\"";
			builder.AppendTextData(fields);
			for (int i = 0; i < labels.Count; i++)
			{
				String data = "\n" + labels[i].ToString();
				builder.AppendTextData(data);
			}
			builder.AppendTextData("\n");
			builder.CloseTextData();

			using (Engine engine = new Engine(true))
			{
				Messages msgs = null;
				engine.XMLScript(path, XMLSourceType.ScriptFile, out msgs);

				foreach (Seagull.BarTender.Print.Message msg in msgs)
					MessageBox.Show(msg.Text, msg.Source.ToString());

				engine.Stop();
			}
		}
	}
}
